using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Reflection;
using Ltf.Remoting.TFAccess;
using Ltf.Remoting.RemoteEvents;

namespace Ltf.MonitorLizard.Forms
{
	[FormType(FormTypes.ExcutableDiff)]

	public partial class ExecutableDiff : Form
	{
		public ExecutableDiff(string[] pathList, LizardEventArgs args)
			:this ()
		{
				if (pathList.Length == 2)
				{
					ShowFile1(pathList[0]);
					ShowFile2(pathList[1]);
					HighLightDifferences();
				}
			
		}



		public ExecutableDiff()
		{
			InitializeComponent();
			btnExit.Focus();
		}

		public ExecutableDiff(string path1, string path2)
			: this()
		{
			ShowFile1(path1);
			ShowFile2(path2);
			HighLightDifferences();
		}

		private void label4_Click(object sender, EventArgs e)
		{

		}

		private void lblFileVersion_Click(object sender, EventArgs e)
		{

		}

		private void ShowFile1(string filepath)
		{
			string fsFilepath = LizardFileAccess.GetFilePath(null, filepath, "left");

			groupBox1.Text = Path.GetFileName(filepath);
			FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fsFilepath);
			lblFileVersion1.Text = fvi.FileVersion;
			txtCompany1.Text = fvi.CompanyName;
			txtDescription1.Text = fvi.FileDescription;
			lblProductVersion1.Text = fvi.ProductVersion;
			try
			{
				AssemblyName asmName = AssemblyName.GetAssemblyName(fsFilepath);
				lblAssemblyVersion1.Text = asmName.Version.ToString();
			}
			catch
			{
				lblAssemblyVersion1.Text = "n/a";
			}
			lblLength1.Text = string.Format("{0:0.00}Kb", ((double)new FileInfo(fsFilepath).Length) / 1000);

			txtPath1.Text = Path.GetDirectoryName(filepath);
		}

		private void ShowFile2(string filepath)
		{
			string fsFilepath = LizardFileAccess.GetFilePath(null, filepath, "right");
			groupBox2.Text = Path.GetFileName(filepath);
			FileVersionInfo fvi = FileVersionInfo.GetVersionInfo(fsFilepath);
			lblFileVersion2.Text = fvi.FileVersion;
			txtCompany2.Text = fvi.CompanyName;
			txtDescription2.Text = fvi.FileDescription;
			lblProductVersion2.Text = fvi.ProductVersion;
			try
			{
				AssemblyName asmName = AssemblyName.GetAssemblyName(fsFilepath);
				lblAssemblyVersion2.Text = asmName.Version.ToString();
			}
			catch
			{
				lblAssemblyVersion2.Text = "n/a";
			}
			lblLength2.Text = string.Format("{0:0.00}Kb", ((double)new FileInfo(fsFilepath).Length) / 1000);

			txtPath2.Text = Path.GetDirectoryName(filepath);
		}

		private void HighLightDifferences()
		{
			Control[] controls1 = new Control[] {lblFileVersion1, lblProductVersion1, lblAssemblyVersion1,
				lblLength1, txtCompany1, txtDescription1};
			Control[] controls2 = new Control[] {lblFileVersion2, lblProductVersion2, lblAssemblyVersion2,
				lblLength2, txtCompany2, txtDescription2};
			for (int i = 0; i < controls1.Length; i++)
			{
				if (controls1[i].Text != controls2[i].Text)
				{
					controls1[i].Font = new Font(controls1[i].Font, FontStyle.Bold);
					controls1[i].ForeColor = System.Drawing.Color.DarkRed;
					controls2[i].Font = new Font(controls1[i].Font, FontStyle.Bold);
					controls2[i].ForeColor = System.Drawing.Color.DarkRed;
				}
			}
		}

		private void ExecutableDiff_DragOver(object sender, DragEventArgs e)
		{

			e.Effect = DragDropEffects.Copy;
		}

		private void ExecutableDiff_DragDrop(object sender, DragEventArgs e)
		{
			try
			{
				string fileName=((string[])e.Data.GetData("FileNameW"))[0];
				string ext = Path.GetExtension(fileName);
				if (ext == ".dll" || ext == ".exe")
				{
					if (e.X < this.Width / 2)
						ShowFile1(fileName);
					else
						ShowFile2(fileName);
					HighLightDifferences();

				}
			}
			catch
			{
			}
		}

		private void btnExit_Click(object sender, EventArgs e)
		{
			this.Close();
		}
	}
}